var js_libraries = [];

var fnOpenEditForm = function(this_element){

	var href_url = this_element.attr("href");

	var dialog_height = $(window).height() - 80;

	//Close all
	$(".ui-dialog-content").dialog("close");

	$.ajax({
		url: href_url,
		data: {
			is_ajax: 'true'
		},
		type: 'post',
		dataType: 'json',
		beforeSend: function() {
			this_element.closest('.flexigrid').addClass('loading-opacity');
		},
		complete: function(){
			this_element.closest('.flexigrid').removeClass('loading-opacity');
		},
		success: function (data) {
			if (typeof CKEDITOR !== 'undefined' && typeof CKEDITOR.instances !== 'undefined') {
					$.each(CKEDITOR.instances,function(index){
						delete CKEDITOR.instances[index];
					});
			}

			LazyLoad.loadOnce(data.js_lib_files);
			LazyLoad.load(data.js_config_files);

			$.each(data.css_files,function(index,css_file){
				load_css_file(css_file);
			});

			//console.log(data);
			var dTitle = (gcConf.state == 'Add' ? 'Tambah' : 'Edit') + ' ' + gcConf.subject;

			var cdlg = $("<div/>").html(data.output).addClass('flexigrid-dlg-form '+window.gcConf.subject_cls).dialog({
				width: 'auto',
				modal: true,
				title: dTitle,
				height: 'auto',
				close: function(){
					if($(this_element).attr('invoke_after'))
					{
						var fn_invoke_after = $(this_element).attr('invoke_after');
						if(typeof window[fn_invoke_after] == 'function')
						{
							window[fn_invoke_after](this_element);	
						}
					}
					$(this).remove();
				},
				open: function(){
					var this_dialog = $(this);
					var dlg_title = this_dialog.closest('.ui-dialog').find('.ui-dialog-title'); 
					if(gcConf.state == 'Read'){
						dlg_title.text(dlg_title.text().replace(/edit/ig,'View'));
						this_dialog.closest('.ui-dialog').find('.tDiv').hide()
					}
					//console.log(dlg_title.text());
					
					if(typeof window.gcConf.onDialogOpen!='undefined')
					{

						if( typeof window.gcConf.onDialogOpen == 'function')
						{
							var frmContainer = $(cdlg).find('.flexigrid.crud-form');
							var frmObj = $(cdlg).find('#crudForm');
							window.gcConf.onDialogOpen(this,cdlg,frmContainer,frmObj);
						}
					}
					$('#cancel-button').click(function(){
						this_dialog.dialog("close");
					});
					// setTimeout(function(){
					// 	// $(cdlg.find('textarea,input').get(0)).focus();
					// 	// console.log('autofocus');
					// 	var title = cdlg.find('.ftitle').text();
					// 	cdlg.dialog('option','title',title);
					// },50);
					
				}
			});
		}
	});
};

var add_edit_button_listener = function () {

	//If dialog AJAX forms is turned on from grocery CRUD config
	if (dialog_forms) {
		//$('.add_button').attr('onclick','');
		$('.edit_button,.add_button').unbind('click');
		$('.edit_button,.add_button').click(function(){
			if($(this).hasClass('add_button'))
				window.gcConf.state = 'Add';
			else
				window.gcConf.state = 'Edit';
			fnOpenEditForm($(this));
			//console.log(window.gcConf);
			return false;
		});

	}
}

var load_css_file = function(css_file) {
	if ($('head').find('link[href="'+css_file+'"]').length == 0) {
		$('head').append($('<link/>').attr("type","text/css")
				.attr("rel","stylesheet").attr("href",css_file));
	}
};

